package com.example.innovate.controller;

/*
    2021.9.13 update/
 */

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.GraduateInfoDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.GraduateStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/graduateStudent")
@Api(tags = "毕业生信息接口")
public class GraduateStudentController {
    @Autowired
    GraduateStudentService graduateStudentService;
    @Autowired
    AdminService adminService;

    //@UserLoginToken
    @GetMapping("/findAllGraduateStudent")
    @ApiOperation("查找所有毕业生信息")
    public Result findAllGraduateStudent(@RequestParam("pageNumber") Integer pageNumber,
                                         @RequestParam("pageSize") Integer pageSize,
                                         @RequestParam("className") String className) throws Exception{
        if(className.equals("superAdmin")){
            List<GraduateInfoDO> list = graduateStudentService.findAllGraduateStudent(pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
        else{
            List<GraduateInfoDO> list = graduateStudentService.findAllGraduateStudent(pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
    }

    //@UserLoginToken
    @PostMapping("/addGraduateStudent")
    @ApiOperation("添加毕业生信息")
    public Result addGraduateStudent(@RequestBody String information,
                                     @RequestParam("className") String className,
                                     @RequestParam("userId") int userId) throws Exception{
        net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(information);
        Object graElse = jo.get("graField");
        String s = graElse.toString();
        jo.remove("graField");
        GraduateInfoDO graduateInfoDO =(GraduateInfoDO) JSONObject.toBean(jo,GraduateInfoDO.class);
        graduateInfoDO.setGraField(s);
        graduateInfoDO.setGraUserId(userId);
        graduateStudentService.addGraduateStudent(graduateInfoDO);
        return new Result(200,"success","添加成功",graduateInfoDO);
    }

    //@UserLoginToken
    @PutMapping("/updateGraduateStudent")
    @ApiOperation("修改毕业生信息")
    public Result updateGraduateStudent(@RequestBody String information) throws Exception{
        JSONObject jo = JSONObject.fromObject(information);
        Object graElse = jo.get("graField");
        String s = graElse.toString();
        jo.remove("graField");
        GraduateInfoDO graduateInfoDO =(GraduateInfoDO) JSONObject.toBean(jo,GraduateInfoDO.class);
        graduateInfoDO.setGraField(s);
        GraduateInfoDO graduateInfoDO1 = graduateStudentService.findGraduateStudentById(graduateInfoDO.getGraId());
        if(graduateInfoDO1!=null){
            graduateStudentService.updateGraduateStudent(graduateInfoDO);
            return new Result(200,"success","修改成功",graduateInfoDO);
        }
        else{
            return new Result(400,"error","所更新毕业生信息不存在",null);
        }

    }

    //@UserLoginToken
    @PostMapping("/deleteGraduateStudent")
    @ApiOperation("删除毕业生信息")
    public Result deleteGraduateStudent(@RequestParam("id") int id) throws Exception{
        GraduateInfoDO graduateInfoDO = graduateStudentService.findGraduateStudentById(id);
        if(graduateInfoDO!=null){
            graduateStudentService.deleteGraduateStudent(id);
            return new Result(200,"success","删除成功","id= "+id);
        }
        else{
            return new Result(400,"error","所删除毕业生信息不存在",null);
        }

    }


    //@UserLoginToken
    @GetMapping("/findByName")
    @ApiOperation("根据姓名查找毕业生信息")
    public Result findGraduateStudent(@RequestParam("name") String name) throws Exception{
        JSONArray array = graduateStudentService.valueList();
        JSONArray array1 = new JSONArray();
        for(int i=0; i<array.size(); i++){
            String s = (String) array.getJSONObject(i).get("name");
            if(s.equals(name)){
                array1.add(array.getJSONObject(i));
            }
        }
        if(array1!=null){
            return new Result(200,"success","查找完毕",array1);
        }
        else{
            return new Result(400,"error","搜索用户不存在","null");
        }
    }
}
